import 'dart:js';

import 'package:flutter/material.dart';
import 'package:flutterdemo/widget/0. codelabs/01. flutter布局基础教程/icon.dart';
import 'package:flutterdemo/widget/0. codelabs/01. flutter布局基础教程/image.dart';
import 'package:flutterdemo/widget/0. codelabs/01. flutter布局基础教程/text.dart';
import 'package:window_location_href/window_location_href.dart';
import 'package:flutterdemo/widget/0. codelabs/01. flutter布局基础教程/columns.dart';
import 'package:flutterdemo/widget/0. codelabs/01. flutter布局基础教程/modifying_axis_size.dart';
import 'package:flutterdemo/widget/0. codelabs/01. flutter布局基础教程/flex.dart';
import 'package:flutterdemo/widget/0. codelabs/01. flutter布局基础教程/expanded.dart';
import 'package:flutterdemo/widget/0. codelabs/01. flutter布局基础教程/sized_box.dart';
import 'package:flutterdemo/widget/0. codelabs/01. flutter布局基础教程/spacer.dart';

import 'package:flutterdemo/widget/1.用户界面/01. 基础的widgets.dart';
import 'package:flutterdemo/widget/1.用户界面/02. 使用material组件.dart';
import 'package:flutterdemo/widget/1.用户界面/03. 处理手势.dart';
import 'package:flutterdemo/widget/1.用户界面/04. 根据用户输入改变widget.dart';
import 'package:flutterdemo/widget/1.用户界面/05. 通过回调得到状态改变的通知.dart';
import 'package:flutterdemo/widget/1.用户界面/06. 购物车例子.dart';
import 'package:flutterdemo/widget/1.用户界面/07. 父组件存储可变状态.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo'),
      routes: routers,
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, this.title}) : super(key: key);

  final String? title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    final href = getHref();
    int? index = href?.indexOf("#");
    if (href != null && index != null && index > 0) {
      String uri = href;
      String key = uri.substring(index + 1, uri.length);
      if (key.isNotEmpty && key.length > 3) {
        var result = Uri.decodeFull(key);
        if (routers.keys.contains(result) && result != "/") {
          Future(() {
            Navigator.pushNamed(this.context, result);
          });
        }
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    var routeLists = routers.keys.toList();
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title!),
      ),
      body: new Container(
        child: new ListView.builder(
          itemBuilder: (context, index) {
            return new InkWell(
              onTap: () {
                Navigator.of(context).pushNamed(routeLists[index]);
              },
              child: new Card(
                child: new Container(
                  alignment: Alignment.centerLeft,
                  margin: EdgeInsets.symmetric(horizontal: 10),
                  height: 50,
                  child: new Text(routers.keys.toList()[index]),
                ),
              ),
            );
          },
          itemCount: routers.length,
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

Map<String, WidgetBuilder> routers = {
  "01. 基础的widgets": (context) {
    return new MyScaffold();
  },
  "02: 使用material组件": (context) {
    return new TutorialHome();
  },
  "03. 处理手势": (context) {
    return new MyButton03();
  },
  "04. 根据用户输入改变widget": (context) {
    return new Counter0104();
  },
  "05. 根据用户输入改变widget:稍微复杂的例子": (context) {
    return new Counter0105();
  },
  "06. 购物车例子": (context) {
    return new Q0106();
  },
  "07. 购物车例子增强": (context) {
    return new Q0107();
  },
  "layout: columns": (context) {
    return new Columns();
  },
  "layout: flex": (context) {
    return new CusFlex();
  },
  "layout: expanded": (context) {
    return new CusExpanded();
  },
  "layout: sizedbox": (context) {
    return new CusSizedBox();
  },
  "layout: spacer": (context) {
    return new CusSpacer();
  },
  "widget: text": (context) {
    return new CusText();
  },
  "widget: icon": (context) {
    return new CusIcon();
  },
  "widget: image": (context) {
    return new CusImage();
  },
  "example: ModifyingAxisSize": (context) {
    return new ModifyingAxisSize();
  }
};

enum Cat { black, white }

extension CatExtension on Cat {
  String? get name {
    switch (this) {
      case Cat.black:
        return 'Mr Black Cat';
      case Cat.white:
        return 'Ms White Cat';
      default:
        return null;
    }
  }

  void talk() {
    print('meow');
  }
}
